package qlsl.androiddesign.view.subview.commonview;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.photoselector.model.PhotoModel;
import com.photoselector.ui.PhotoSelectorActivity;
import com.qlsl.androiddesign.kuaiwa.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import qlsl.androiddesign.activity.commonactivity.PhotoAddActivity;
import qlsl.androiddesign.adapter.baseadapter.BaseAdapter;
import qlsl.androiddesign.adapter.commonadapter.PictureAddAdapter;
import qlsl.androiddesign.constant.IntentCodeConstant;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 公用图片添加页<br/>
 * 需要传入的键：max_count,files<br/>
 * 传入的值类型： int,List <File> <br/>
 * 传入的值含义：最大图片个数,图片流<br/>
 * 是否必传 ：否，是
 */
public class PhotoAddView extends FunctionView<PhotoAddActivity> {

	private int max_count = 9;

	private GridView gridView;

	private List<File> pictureFiles = new ArrayList<File>();

	private ArrayList<PhotoModel> selectPhotos = new ArrayList<PhotoModel>();

	public PhotoAddView(PhotoAddActivity activity) {
		super(activity);
		setContentView(R.layout.activity_photo_add);
	}

	protected void initView(View view) {
		max_count = activity.getIntent().getIntExtra("max_count", max_count);
		setTitle("添加图片");
		gridView = (GridView) view.findViewById(R.id.gridView);
	}

	protected void initData() {
		setGridViewData();
	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_add:
			doClickSelectView();
			break;
		case R.id.btn_ok:
			doClickOKView();
			break;
		case R.id.iv_delete:
			doClickDeleteView(view);
			break;
		}
	}

	@SuppressWarnings("unchecked")
	private void setGridViewData() {
		List<File> pictureFiles = (List<File>) activity.getIntent().getSerializableExtra("files");
		if (pictureFiles != null) {
			this.pictureFiles = pictureFiles;
			for (File file : pictureFiles) {
				PhotoModel item = new PhotoModel();
				item.setOriginalPath(file.getAbsolutePath());
				item.setChecked(true);
				selectPhotos.add(item);
			}
		}
		notifyDataSetChanged();
	}

	private void doClickSelectView() {
		if(gridView.getAdapter().getCount()==max_count+1){
			showToast("不能超过限定数量");
			return;
		}
		Intent intent = new Intent(activity, PhotoSelectorActivity.class);
		intent.putExtra(PhotoSelectorActivity.KEY_MAX, 1);
//		intent.putExtra(PhotoSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, selectPhotos);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivityForResult(intent, IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED);

	}

	private void doClickOKView() {
		Intent intent = new Intent();
		pictureFiles.remove(pictureFiles.size() - 1);
		intent.putExtra("files", (Serializable) pictureFiles);
		activity.setResult(0, intent);
		activity.finish();
	}

	@SuppressWarnings("unchecked")
	public void onActivityResultOld(int requestCode, int resultCode, Intent data) {
		if (requestCode == IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle bundle = data.getExtras();
				if (bundle == null) {
					showToast("无法加载图片");
					return;
				}
				selectPhotos = (ArrayList<PhotoModel>) bundle.getSerializable("photos");
				pictureFiles.clear();
				for (PhotoModel photoModel : selectPhotos) {
					String path = photoModel.getOriginalPath();
					File file = new File(path);
					if (file.exists()) {
						pictureFiles.add(file);
					}
				}
				pictureFiles.add(null);
				notifyDataSetChanged();
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle bundle = data.getExtras();
				if (bundle == null) {
					showToast("无法加载图片");
					return;
				}
				List<PhotoModel> selectPhotos = (List<PhotoModel>) bundle
						.getSerializable(PhotoSelectorActivity.KEY_RESULT);
				if (selectPhotos.size() > 0) {
					String path = selectPhotos.get(0).getOriginalPath();
					cropPhoto(path);
				}
			}
		} else if (requestCode == IntentCodeConstant.CROP_PHOTO) {
			if (data == null) {
				return;
			}
			Bitmap bitmap = data.getExtras().getParcelable("data");
			File file = saveBitmapToFile(bitmap);
			
			PhotoModel item = new PhotoModel();
			item.setOriginalPath(file.getAbsolutePath());
			item.setChecked(true);
			
			selectPhotos.add(item);
			pictureFiles.add(file);
			
			notifyDataSetChanged();
		}
	};

	@SuppressWarnings("unchecked")
	private void notifyDataSetChanged() {
		BaseAdapter<File> adapter = (BaseAdapter<File>) gridView.getAdapter();
		if (adapter == null) {
			pictureFiles.add(null);
			adapter = new PictureAddAdapter(activity, pictureFiles);
			gridView.setAdapter(adapter);
		} else {
			pictureFiles.remove(null);
			pictureFiles.add(null);
			adapter.notifyDataSetChanged();
		}

	}

	private void doClickDeleteView(View view) {
		int position = gridView.getPositionForView(view);
		selectPhotos.remove(position);
		pictureFiles.remove(position);
		PictureAddAdapter adapter = (PictureAddAdapter) gridView.getAdapter();
		adapter.notifyDataSetChanged();
	}

}
